<?php
require_once 'models/orm.inc';

class TCMSPageCategoryModel extends TCandyModel {
	protected function afterConstruct(){
		$this->_conf_classname_ = 'TMSCategoryPageAssociation';
	}
	
	public function MovePage(TMSPage $page, array $categories){
		$oldcategories = array();
		foreach ($page->Categories as $cat)
			$oldcategories[] = $cat->Id;

		foreach ($categories as $catid)
			if (!in_array($catid,$oldcategories))
				$this->CreateItem(array('Page'=>$page->Id,'Category'=>$catid));
		
		foreach ($page->Categories as $cat)
			if (!in_array($cat->Id, $categories)){
				$c = new TMSCategoryPageAssociation();
				$c->Page = $page->Id;
				$c->Category = $cat->Id;
				$this->DeleteObject($c);
			}
	}
} 